/**
 * 
 */
package info.niwota.frames;

import android.content.ContentResolver;
import android.database.Cursor;

/**
 * @author qiangli
 * 
 */
public class RecentImageList implements IImageList {
	@SuppressWarnings("unused")
	private static final String TAG = "RecentImageList";
	
	private static final int CACHE_CAPACITY = 512;
	
	private final LruCache<Integer, IImage> mCache = new LruCache<Integer, IImage>(
			CACHE_CAPACITY);
	private Cursor mCursor;
	private boolean mCursorDeactivated = false;
	private ContentResolver mContentResolver;

	/**
	 * @param contentResolver
	 * 
	 */
	public RecentImageList(ContentResolver contentResolver) {
		mContentResolver = contentResolver;
	}

	public void init() {
		mCursor = ImageUtil.queryRecent(mContentResolver);
	}

	public void close() {
		if (mCursor != null) {
			mCursor.close();
			mCursor = null;
		}
	}

	public int getCount() {
		Cursor cursor = getCursor();
		if (cursor == null) {
			return 0;
		}
		synchronized (this) {
			//return cursor.getCount();
			//TODO settings
			int cnt = cursor.getCount();
			return cnt;
			//return Math.min(cnt, MAX_COUNT);
		}
	}

	public boolean isEmpty() {
		return getCount() == 0;
	}

	private Cursor getCursor() {
		synchronized (this) {
			if (mCursor == null) {
				return null;
			}
			if (mCursorDeactivated) {
				mCursor.requery();
				mCursorDeactivated = false;
			}
			return mCursor;
		}
	}

	public IImage getImageAt(int i) {
		IImage result = mCache.get(i);
		if (result == null) {
			Cursor cursor = getCursor();
			if (cursor == null) {
				return null;
			}
			synchronized (this) {
				result = cursor.moveToPosition(i) ? loadImageFromCursor(cursor)
						: null;
				mCache.put(i, result);
			}
		}
		return result;
	}

	public boolean removeImageAt(int i) {
		return false;
	}

	private static final int INDEX_ID = 0;
//	private static final int INDEX_DATA_PATH = 1;
//	private static final int INDEX_DATE_TAKEN = 2;
//	private static final int INDEX_MINI_THUMB_MAGIC = 3;
//	private static final int INDEX_ORIENTATION = 4;
//	private static final int INDEX_TITLE = 5;
//	private static final int INDEX_MIME_TYPE = 6;
//	private static final int INDEX_DATE_MODIFIED = 7;

	private IImage loadImageFromCursor(Cursor cursor) {
		long id = cursor.getLong(INDEX_ID);
		// String dataPath = cursor.getString(INDEX_DATA_PATH);
		// long dateTaken = cursor.getLong(INDEX_DATE_TAKEN);
		// if (dateTaken == 0) {
		// dateTaken = cursor.getLong(INDEX_DATE_MODIFIED) * 1000;
		// }
		// long miniThumbMagic = cursor.getLong(INDEX_MINI_THUMB_MAGIC);
		// int orientation = cursor.getInt(INDEX_ORIENTATION);
		// String title = cursor.getString(INDEX_TITLE);
		// String mimeType = cursor.getString(INDEX_MIME_TYPE);
		// if (title == null || title.length() == 0) {
		// title = dataPath;
		// }
		// String displayName = title;
		return new UriImage(mContentResolver, ImageUtil.contentUri(id));
		// return new UriImage(this, mContentResolver, id, cursor.getPosition(),
		// contentUri(id), dataPath, miniThumbMagic, mimeType, dateTaken,
		// title, displayName, orientation);
	}

}
